﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VoteUphold;
using System.IO;
using System.Web;
using System.Net;

namespace Vote
{
    public partial class FrmCsdnLogin : Form
    {
        public FrmCsdnLogin()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string postString = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=" +
                HttpUtility.UrlEncode(vve.__VIEWSTATE)
                +
                "&__EVENTVALIDATION="+vve.__EVENTVALIDATION
                   +
                "&ctl00$CPH_Content$tb_LoginNameOrLoginEmail=feiazifeiazi@163.com&"
            +"ctl00$CPH_Content$tb_Password=qsiofg4nui897574&ctl00$CPH_Content$tb_ExPwd=" + 
            this.textBox1.Text.Trim() + "&ClientKey="+clientKey+"&from="+
            "http://hi.csdn.net/&PrePage=http://www.csdn.net/&MailParameters=&ctl00$CPH_Content$Image_Login.x=0&ctl00$CPH_Content$Image_Login.y=0";
           
            try
            { //ctl 0$CPH_Content$tb_Password
                string s = vu.httpClient.PostData(vu.voteUrl.AdressVoteSend, postString, Encoding.UTF8,Encoding.UTF8);
                this.rtbResult.AppendText(s);
            }
            catch (Exception e我)
            {
                MessageBox.Show(e我.Message);
            }
           
        }

        VUvote vu = null;
        VE__VIEWSTATE__EVENTVALIDATION vve = null;
        string clientKey = "";
        private void FrmCsdnLogin_Load(object sender, EventArgs e)
        {

            string path = Application.StartupPath;
            path = path.Substring(0, path.Length - "\\bin\\Debug".Length) + "\\VoteUrlCsdn.xml";
            vu = new VUvote(new VoteUrl(path));

            byte[] responseData = vu.httpClient.DownloadData(vu.voteUrl.AdressVoteShow);
            string srcString = Encoding.UTF8.GetString(responseData);
            vve = VUvote.getViewData.Get__VIEWSTATE__EVENTVALIDATION(srcString);
            clientKey=VUvote.getViewData.GetData(srcString, "name=\"ClientKey\" value=\"", "\" />", 0);
            GetRand();
            this.textBox2.Text = "http://www.csdn.net";

        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] responseData = vu.httpClient.DownloadData(this.textBox2.Text);
            string srcString = Encoding.UTF8.GetString(responseData);
            this.rtbResult.AppendText(srcString);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            GetRand();
        }

        private void GetRand()
        {
            if (File.Exists("szz.gif"))
            {
                File.Delete("szz.gif");
            }
            vu.httpClient.DownloadFile(new Uri(vu.voteUrl.AdressCode), "szz.gif");
            FileInfo fi = new FileInfo("szz.gif");
            if (fi != null)
            {
                fi.Attributes = FileAttributes.Hidden;
            }
            pictureBox1.LoadAsync("szz.gif");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string s = this.textBox3.Text.Trim();
            vu = new VUvote(new VoteUrl());
            vu.httpClient.Cookies= new CookieContainer();
            string[] sarry = s.Split('=',';');
            for (int i = 0; i < sarry.Length;i++)
            {
                vu.httpClient.Cookies.Add(new Uri("http://hi.csdn.net/"), new Cookie(sarry[i].Trim(), sarry[++i].Trim()));
            }
            

        }
    }
}
